// InPlaceDateTime.cpp : implementation file
//

#include "pch.h"
#include "GridCtrl.h"
#include "InPlaceDateTime.h"

// CInPlaceDateTime

IMPLEMENT_DYNAMIC(CInPlaceDateTime, CDateTimeCtrl)
CInPlaceDateTime::CInPlaceDateTime()
{ 
}

CInPlaceDateTime::~CInPlaceDateTime()
{
}


BEGIN_MESSAGE_MAP(CInPlaceDateTime, CDateTimeCtrl)
	ON_WM_KEYDOWN()
	ON_WM_KEYUP()
	ON_WM_KILLFOCUS()
END_MESSAGE_MAP()

// CInPlaceDateTime message handlers
CInPlaceDateTime::CInPlaceDateTime(CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID,
                           int nRow, int nColumn, CString sInitText, 
                           UINT nFirstChar)
{
    m_sInitText     = sInitText;
    m_nRow          = nRow;
    m_nColumn       = nColumn;
    m_nLastChar     = 0; 
    m_bExitOnArrows = (nFirstChar != VK_LBUTTON);    // If mouse click brought us here,
      
	DWORD dwDTStyle = WS_BORDER|WS_CHILD|WS_VISIBLE| DTS_UPDOWN|DTS_SHORTDATEFORMAT;
	
	if (!Create(dwDTStyle, rect, pParent, nID)) return;

	CString s = sInitText ;
	SYSTEMTIME st ;	
	memset(&st, 0, sizeof(st));
	if(dwStyle == 1)
	{		
		st.wYear = _ttoi(s);
		s = s.Mid(5); 
		st.wMonth = _ttoi(s);
		s = s.Mid(3);
		st.wDay = _ttoi(s);
		s = s.Mid(3);		
		SetFormat(_T("yyyy-MM-dd")); 
		SetTime(&st);		
	}
	else if(dwStyle == 2)
	{	
		GetLocalTime(&st); 		
		if( s != _T("") )st.wHour = _ttoi(s);
		s = s.Mid(3);
		if( s != _T("") )st.wMinute = _ttoi(s);
		s = s.Mid(3);
		if( s != _T("") )st.wSecond = _ttoi(s);	

		SetFormat(_T("HH:mm:ss")); 
		SetTime(&st);		
	}
	else if(dwStyle == 3)
	{		
		st.wYear = _ttoi(s);
		s = s.Mid(5); 
		st.wMonth = _ttoi(s);
		s = s.Mid(3);
		st.wDay = _ttoi(s);
		s = s.Mid(3);
		st.wHour = _ttoi(s);
		s = s.Mid(3);
		st.wMinute = _ttoi(s);
		s = s.Mid(3);
		st.wSecond = _ttoi(s);			
		SetFormat(_T("yyyy-MM-dd HH:mm:ss"));
		SetTime(&st); 	
	}

	SetFont(pParent->GetFont());
        
    SetFocus();

}

void CInPlaceDateTime::EndEdit()
{
    CString str;
    GetWindowText(str);
 
    // Send Notification to parent
    GV_DISPINFO dispinfo;

    dispinfo.hdr.hwndFrom = GetSafeHwnd();
    dispinfo.hdr.idFrom   = GetDlgCtrlID();
    dispinfo.hdr.code     = GVN_ENDLABELEDIT;
 
    dispinfo.item.mask    = LVIF_TEXT|LVIF_PARAM;
    dispinfo.item.row     = m_nRow;
    dispinfo.item.col     = m_nColumn;
    dispinfo.item.szText  = str;
    dispinfo.item.lParam  = (LPARAM) m_nLastChar; 
 
    CWnd* pOwner = GetOwner();
    if (IsWindow(pOwner->GetSafeHwnd()))
        pOwner->SendMessage(WM_NOTIFY, GetDlgCtrlID(), (LPARAM)&dispinfo );
 
    // Close this window (PostNcDestroy will delete this)
    PostMessage(WM_CLOSE, 0, 0);
}

void CInPlaceDateTime::PostNcDestroy()
{
	delete this;
	CDateTimeCtrl::PostNcDestroy();	
}

void CInPlaceDateTime::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	if ((nChar == VK_PRIOR || nChar == VK_NEXT ||
        nChar == VK_DOWN  || nChar == VK_UP   ||
        nChar == VK_RIGHT || nChar == VK_LEFT) &&
        (m_bExitOnArrows || GetKeyState(VK_CONTROL) < 0))
    {
        m_nLastChar = nChar;
        GetParent()->SetFocus();
        return;
    }
	CDateTimeCtrl::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CInPlaceDateTime::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	// TODO: Add your message handler code here and/or call default

	CDateTimeCtrl::OnKeyUp(nChar, nRepCnt, nFlags);
}

void CInPlaceDateTime::OnKillFocus(CWnd* pNewWnd)
{
	CDateTimeCtrl::OnKillFocus(pNewWnd);
	EndEdit();
}
